/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dao.exceptions.NonexistentEntityException;
import entities.ProblemStats;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entities.Problem;
import vo.VOProblemStats;

/**
 *
 * @author santiagobaldrich
 */
public class ProblemStatsJpaController {

    public ProblemStatsJpaController()
    {
        emf = Persistence.createEntityManagerFactory("UNOJModelPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager()
    {
        return emf.createEntityManager();
    }

    public void create(ProblemStats problemStats)
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            Problem problem = problemStats.getProblem();
            if (problem != null)
            {
                problem = em.getReference(problem.getClass(), problem.getIdProblem());
                problemStats.setProblem(problem);
            }
            em.persist(problemStats);
            if (problem != null)
            {
                problem.getProblemStatsCollection().add(problemStats);
                problem = em.merge(problem);
            }
            em.getTransaction().commit();
        } finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    public void edit(ProblemStats problemStats) throws NonexistentEntityException, Exception
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            ProblemStats persistentProblemStats = em.find(ProblemStats.class, problemStats.getIdProblemStats());
            Problem problemOld = persistentProblemStats.getProblem();
            Problem problemNew = problemStats.getProblem();
            if (problemNew != null)
            {
                problemNew = em.getReference(problemNew.getClass(), problemNew.getIdProblem());
                problemStats.setProblem(problemNew);
            }
            problemStats = em.merge(problemStats);
            if (problemOld != null && !problemOld.equals(problemNew))
            {
                problemOld.getProblemStatsCollection().remove(problemStats);
                problemOld = em.merge(problemOld);
            }
            if (problemNew != null && !problemNew.equals(problemOld))
            {
                problemNew.getProblemStatsCollection().add(problemStats);
                problemNew = em.merge(problemNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex)
        {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0)
            {
                Integer id = problemStats.getIdProblemStats();
                if (findProblemStats(id) == null)
                {
                    throw new NonexistentEntityException("The problemStats with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            ProblemStats problemStats;
            try
            {
                problemStats = em.getReference(ProblemStats.class, id);
                problemStats.getIdProblemStats();
            } catch (EntityNotFoundException enfe)
            {
                throw new NonexistentEntityException("The problemStats with id " + id + " no longer exists.", enfe);
            }
            Problem problem = problemStats.getProblem();
            if (problem != null)
            {
                problem.getProblemStatsCollection().remove(problemStats);
                problem = em.merge(problem);
            }
            em.remove(problemStats);
            em.getTransaction().commit();
        } finally
        {
            if (em != null)
            {
                em.close();
            }
        }
    }

    public List<ProblemStats> findProblemStatsEntities()
    {
        return findProblemStatsEntities(true, -1, -1);
    }

    public List<ProblemStats> findProblemStatsEntities(int maxResults, int firstResult)
    {
        return findProblemStatsEntities(false, maxResults, firstResult);
    }

    private List<ProblemStats> findProblemStatsEntities(boolean all, int maxResults, int firstResult)
    {
        EntityManager em = getEntityManager();
        try
        {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ProblemStats.class));
            Query q = em.createQuery(cq);
            if (!all)
            {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally
        {
            em.close();
        }
    }

    public ProblemStats findProblemStats(Integer id)
    {
        EntityManager em = getEntityManager();
        try
        {
            return em.find(ProblemStats.class, id);
        } finally
        {
            em.close();
        }
    }

    public int getProblemStatsCount()
    {
        EntityManager em = getEntityManager();
        try
        {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ProblemStats> rt = cq.from(ProblemStats.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally
        {
            em.close();
        }
    }

    public ProblemStats findByProblem(Problem p)
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            Query q = em.createQuery("Select ps from ProblemStats ps where ps.problem = :problem");
            q.setParameter("problem", p);
            return (ProblemStats)q.getSingleResult();
        }
        catch (Exception e)
        {
            System.out.println("Exception: " + e.getMessage());
            return null;
        }
    }

    public List<ProblemStats> findAll()
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            Query q = em.createNamedQuery("ProblemStats.findAll");
            return (List<ProblemStats>)q.getResultList();
        } catch (Exception e)
        {
            System.out.println("Exception at ProblemStatsJPAController.findAll: "+e.getMessage());
            return null;
        }
    }

}
